package com.santiao.publics.ui;



import android.content.Context;
import android.content.Intent;

import android.os.Build;
import android.os.Bundle;

import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;


import com.santiao.fuel.R;
import com.santiao.publics.base.BaseActivity;
import com.yl.adsdk.YlLib;

import org.jetbrains.annotations.Nullable;

public class WebViewActivity extends BaseActivity {
    private WebView web_view;

    public static void gotoActivity(Context context, String title,String type) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.public_activity_webview);
        initView();
        initData();
    }
    boolean bo = true;
    private void initView() {
        initTitle();
        String type = getIntent().getStringExtra("type");
        web_view = findViewById(R.id.web_view);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            web_view.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View view, int i, int i1, int i2, int i3) {
                    if(bo){
                        bo= false;
                        try {
                            YlLib.setCheckPrivacy();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        web_view.getSettings().setJavaScriptEnabled(true);
        web_view.getSettings().setDomStorageEnabled(true);

        if("yhxy".equals(type)){
            if(getApplicationContext().getString(R.string.app_name).indexOf("全国")>-1){
                web_view.loadUrl("http://niukebang.cn/" + getPackageName() + "/yhxy2.html");//用户条款
            }else {
                web_view.loadUrl("http://niukebang.cn/" + getPackageName() + "/yhxy.html");//用户条款
            }

        }else if("ystk".equals(type)){
            if(getApplicationContext().getString(R.string.app_name).indexOf("全国")>-1){
                web_view.loadUrl("http://niukebang.cn/" + getPackageName() + "/ystk2.html");//隐私协议
            }else {
                web_view.loadUrl("http://niukebang.cn/" + getPackageName() + "/ystk.html");//隐私协议
            }
        }else if("yjfk".equals(type)){//意见反馈
            web_view.loadUrl("http://niukebang.cn/"+getPackageName()+"/yjfk.html?v=2");
            web_view.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

        }



        TextView tv_title = findViewById(R.id.tv_title);
        String title = getIntent().getStringExtra("title");

        tv_title.setText(title);
    }

    private void initData() {
//        web_view.loadUrl("http://niukebang.cn/com.dazibao.quick/yszc.html");

    }
}
